﻿namespace Triad.Controls
{
    using System;
    using System.ComponentModel;
    using System.Data;

    public partial class tfListView
    { 
        
        #region "Declaration Section" 
        //Type for export format 
        /// <summary>
        /// Excel, CSV, etc.)
        /// </summary>
        public enum ExportTypeEnum 
        {
            /// <summary>
            /// 
            /// </summary>
            Excel,
            /// <summary>
            /// 
            /// </summary>
            CSV 
        } 
        
        
        /// <summary>
        /// Seperator Type (Comman, Tab, etc)
        /// </summary>
        public enum SeparatorTypeEnum 
        {
            /// <summary>
            /// 
            /// </summary>
            Comma,
            /// <summary>
            /// 
            /// </summary>
            TAB 
        } 
        
        private DataView c_ExportData; 
        private const string C_HTTP_HEADER_CONTENT = "Content-Disposition"; 
        private const string C_HTTP_ATTACHMENT = "attachment;filename="; 
        private const string C_HTTP_CONTENT_TYPE_OCTET = "application/octet-stream"; 
        private const string C_HTTP_CONTENT_TYPE_EXCEL = "application/ms-excel"; 
        private const string C_HTTP_CONTENT_LENGTH = "Content-Length"; 
        private const string C_ERROR_NO_RESULT = "Data not found."; 
        #endregion 

        /// <summary>
        /// Assign dataview for exporting data. Dataset or Datatable can also be used.
        /// </summary>
        /// <value>The dataview.</value>
        /// <remarks>
        /// Dataview is used especially when sort order needs to be preseved in the export.
        /// 
        /// Author: tim.fischer,   8/25/2008 - 2:59 PM
        /// Modified By: 
        /// </remarks>
        /// <example>[For Example: ExportButton1.Dataview=Dataset.Tables(0).DefaultView]</example> 
        [Bindable(false), Category("ExportButton"), Browsable(false)] 
        public DataView Dataview { 
            get { 
                var o = ViewState["data"]; 
                if (o == null) { 
                    return null; 
                }
                var t = (DataTable)o; 
                return t.DefaultView;
            } 
            
            set { ViewState.Add("data", value.Table); } 
        }

        /// <summary>
        /// Gets or sets a value indicating whether to enable export functionality.
        /// </summary>
        /// <value><c>true</c> if enabled; otherwise, <c>false</c>.</value>
        /// <remarks>
        /// Author: tim.fischer,   8/26/2008 - 9:07 AM
        /// Modified By: 
        /// </remarks>
        [Bindable(false), DefaultValue(false), Category("ExportButton"), Description("Enable the Export Button on the grid."), Browsable(true),
         DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)
        ]
        public Boolean EnableExport
        {
            get
            {
                return Convert.ToBoolean(ViewState["eExport"]);
            } 
            set
            {
                ViewState.Add("eExport", value);
            } 
        }

        /// <summary>
        /// Gets or sets the file name to export.
        /// </summary>
        /// <value>The file name to export.</value>
        /// <remarks>
        /// Author: tim.fischer,   8/25/2008 - 3:01 PM
        /// Modified By: 
        /// </remarks>
        [Bindable(false), Category("ExportButton"), Description("File name to download."), Browsable(true)] 
        public string FileNameToExport { 
            get { 
                object o = ViewState["filename"]; 
                if (o == null) { 
                    return "ExportData.csv"; 
                    //default (CSV extension used as all other default properties return CSV and Comma) 
                }
                return (string)o;
            } 
            
            set { 
                if (value == null || value.Trim().Length == 0) { 
                    throw new ArgumentNullException(FileNameToExport, "Provide valid file name for export."); 
                } 
                ViewState.Add("filename", value); 
            } 
        } 
        
        /// <summary>
        /// Set ExportType to either EXCEL or CSV save the exported data.
        /// </summary>
        /// <value>The type of the export.</value>
        /// <remarks>
        /// Author: tim.fischer,   8/25/2008 - 3:01 PM
        /// Modified By: 
        /// </remarks>
        [Bindable(false), Category("ExportButton"), Description("File format to export (Excel or CSV)."), Browsable(true), DefaultValue("CSV")] 
        public ExportTypeEnum ExportType { 
            get { 
                object obj = ViewState["exporttype"]; 
                if (obj == null) { 
                    return ExportTypeEnum.CSV; 
                    //default 
                }
                return (ExportTypeEnum)obj;
            }

            set { ViewState["exporttype"] = value; } 
        } 

        /// <summary>
        /// Set valid separator if "," is not required
        /// </summary>
        /// <value>The separator.</value>
        /// <remarks>
        /// Author: tim.fischer,   8/25/2008 - 3:02 PM
        /// Modified By: 
        /// </remarks>
        [Bindable(false), Category("ExportButton"), Description("Usually Comma can be used for CSV and Tab for Excel.")] 
        public SeparatorTypeEnum Separator { 
            get {
                object obj = ViewState["separator"]; 
                if (obj == null) { 
                    return SeparatorTypeEnum.Comma; 
                }
                return (SeparatorTypeEnum)obj;
            } 
            set { ViewState["separator"] = value; } 
        } 
        
        /// <summary>
        /// Gets or sets the delimiter. Valid delimiters like single or double quote can be used.
        /// </summary>
        /// <value>The delimiter.</value>
        /// <remarks>
        /// Author: tim.fischer,   8/25/2008 - 3:02 PM
        /// Modified By: 
        /// </remarks>
        [Bindable(false), Category("ExportButton"), Description("Single or Double quoted can be used to delimit data items.")] 
        public string Delimiter { 
            get { 
                object o = ViewState["delimiter"]; 
                if (o == null) { 
                    return string.Empty; 
                }
                return (string)o;
            } 
            
            set { ViewState.Add("delimiter", value); } 
        } 

        /// <summary>
        /// Gets or sets the char set. (Valid Character set for exporting international characters)
        /// </summary>
        /// <value>The char set.</value>
        /// <remarks>
        /// Author: tim.fischer,   8/25/2008 - 3:02 PM
        /// Modified By: 
        /// </remarks>
        [Bindable(false), Category("ExportButton"), Description("Used for various character sets. (Ex: 'iso-8859-2' used for Cenral Europian Characters)")] 
        public string CharSet { 
            get { 
                object o = ViewState["charset"]; 
                if (o == null) { 
                    return string.Empty; 
                }
                return (string)o;
            } 
            
            set { ViewState.Add("charset", value); } 
        } 
        
        /// <summary>
        /// Exports data to a custom format 
        /// </summary>
        /// <remarks>
        /// Author: tim.fischer,   8/25/2008 - 3:03 PM
        /// Modified By: 
        /// </remarks>
        public void CustomExport() 
        { 
            System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; 
            response.Clear(); 
            // Add the header that specifies the default filename 
            // for the Download/SaveAs dialog 
            response.AddHeader(C_HTTP_HEADER_CONTENT, C_HTTP_ATTACHMENT + FileNameToExport); 
            
            // Specify that the response is a stream that cannot be read _ 
            // by the client and must be downloaded 
            response.ContentType = ExportType == ExportTypeEnum.CSV ? C_HTTP_CONTENT_TYPE_OCTET : C_HTTP_CONTENT_TYPE_EXCEL; 
            
            var exportContent = string.Empty;
            c_ExportData = ((DataTable)GetMyDataSource(true)).DefaultView;// Dataview; 
            if (((c_ExportData != null)) && c_ExportData.Table.Rows.Count > 0) { 
                exportContent = Separator == SeparatorTypeEnum.TAB ? DataConversion.ConvertDataViewToString(c_ExportData, Delimiter, "\t", true) : DataConversion.ConvertDataViewToString(c_ExportData, Delimiter, ",", true); 
            } 
            if (exportContent.Length <= 0) { 
                exportContent = C_ERROR_NO_RESULT; 
            } 
            
            var encoding = new System.Text.UnicodeEncoding(); 
            response.AddHeader(C_HTTP_CONTENT_LENGTH, encoding.GetByteCount(exportContent).ToString()); 
            response.BinaryWrite(encoding.GetBytes(exportContent)); 
            response.Charset = CharSet; 
            
            // Stop execution of the current page 
            response.End(); 
        } 
    } 
} 